/*
 * @lc app=leetcode.cn id=8 lang=javascript
 *
 * [8] 字符串转换整数 (atoi)
 */

// @lc code=start
/**
 * @param {string} s
 * @return {number}
 */
var myAtoi = function (s) {
    // s = "   +0 123"
    let negative = ''
    let ans = 0
    for (let i = 0; i < s.length; i++) {
        if (s[i] == ' ' && negative == '') {
            continue
        } else if (s[i] == '+' && negative == '') {
            negative = '+'
        } else if (s[i] == '-' && negative == '') {
            negative = '-'
        } else if (s[i] >= '0' && s[i] <= '9') {
            ans = ans * 10 + parseInt(s[i])
            if (negative == '') negative = 'int'
        } else break
    }
    if (negative == '-') ans = -ans
    let min = -Math.pow(2, 31)
    let max = Math.pow(2, 31) - 1
    if (ans <= min) ans = min
    else if (ans >= max) ans = max
    return ans

};
// @lc code=end
// @after-stub-for-debug-begin
module.exports = myAtoi;
// @after-stub-for-debug-end